package jp.co.piratealliance.pirateAllianceWeb.service.goods.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.co.piratealliance.pirateAllianceWeb.dao.goods.GoodsDao;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.AreaInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.goods.GoodsInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.goods.PayTypeInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.goods.ResourceTypeInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.service.goods.GoodsService;


/**
*
* copyright   pirate alliance team 2014
* @author     lunxing
*
*/

public class GoodsServiceImpl implements GoodsService {

	private GoodsDao goodsDao;

	public List<GoodsInfoDto> getGoodsList(int goods_type_id, String area_id, String language_id, int start_row, int end_row) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("goods_type_id", goods_type_id);
		paramMap.put("area_id", area_id);
		paramMap.put("language_id", language_id);
		paramMap.put("start_row", start_row);
		paramMap.put("end_row", end_row);
		return goodsDao.selectGoods(paramMap);
	}

	public GoodsInfoDto getGoodsByOrderId(String orderId) {
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("order_id", orderId);

		return goodsDao.selectGoodsByOrderId(paramMap);
	}

	public List<AreaInfoDto> getAreaListByLevel(String language_id, int level) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("language_id", language_id);
		paramMap.put("level", level);
		return goodsDao.selectAreaByLevel(paramMap);
	}

	public List<String> getAreaIDListByName(String name) {
		return goodsDao.selectAreaIDByName(name);
	}

	public AreaInfoDto getAreaByAreaIDAndLanguageId(String area_id, String language_id) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("area_id", area_id);
		paramMap.put("language_id", language_id);
		return goodsDao.selectAreaByAreaIDAndLanguageId(paramMap);
	}

	public void insertResourceInfo(GoodsInfoDto goodsInfoForResource){
		//String goodsIdSeq = CommonHelper.getSequence(Constants.TB_GOODS);
		//goodsInfoForResource.setGoods_id(goodsIdSeq);//商品Id
		goodsDao.insertGoodsOfResourceInfo(goodsInfoForResource);//插入商品表
		goodsDao.insertResourceLanguageInfo(goodsInfoForResource);//插入商品详细
		goodsDao.insertGoodsResourceTypeInfo(goodsInfoForResource);//插入商品と資源商品分類関連表
	}

	public Map<String, String> getCurrencyUnitIdMap(){
		Map<String,String> currencyUnitIdMap = new HashMap<String,String>();
		List<GoodsInfoDto> goodsInfoList = goodsDao.selectGoodsInfo();
		for(GoodsInfoDto dto:goodsInfoList){
			currencyUnitIdMap.put(String.valueOf(dto.getCurrency_unit_id()), dto.getName());
		}
		return currencyUnitIdMap;
	}

	public Map<String, String> getPayTypeMap(){
		Map<String,String> payTypeMap = new HashMap<String,String>();
		List<PayTypeInfoDto> payTypeInfos = goodsDao.selectPayTypeInfo();
		for(PayTypeInfoDto dto:payTypeInfos){
			payTypeMap.put(String.valueOf(dto.getPay_type_id()), dto.getDescriptionx());
		}
		return payTypeMap;
	}
	
	public void updateResourceInfo(GoodsInfoDto goodsInfoForResource){
		goodsDao.updateGoodsOfResourceInfo(goodsInfoForResource);
		goodsDao.updateResourceInfo(goodsInfoForResource);
	}


	public List<ResourceTypeInfoDto> getAllResourceTypeList() {
		return goodsDao.selectAllResourceType();
	}

	public List<ResourceTypeInfoDto> getResourceTypeListByGoodsId(String goods_id) {
		return goodsDao.selectGoodsResourceTypeByGoodsId(goods_id);
	}


	//商品登録画面に、該当ユーザーの全部商品情報を検索する
	public List<GoodsInfoDto> getGoodsListByUserId(String user_id){
		return goodsDao.selectGoodsListByUserId(user_id);
	}

	
	
	
	public String getGoodsResourceTypes(String goodsId){
		String resourceTypes="";
		List<ResourceTypeInfoDto> resourceTypeInfoList = goodsDao.selectGoodsResourceTypeByGoodsId(goodsId);
		for(ResourceTypeInfoDto resourceTypeInfo : resourceTypeInfoList){
			resourceTypes = resourceTypes + resourceTypeInfo.getResource_type_id() +",";
		}
		return resourceTypes;
	}
	//画面の商品情報を登録
	public String saveGoodsInfo(List<GoodsInfoDto> goodsInfoList,String userId,String areaId,String selectedResourceTypes,int payTypeId,String goodsId){
		//商品表
		GoodsInfoDto goodsBaseInfo = new GoodsInfoDto();
		//String goodsIdSeq = CommonHelper.getSequence(Constants.TB_GOODS);
		goodsBaseInfo.setGoods_id(goodsId);//商品Id
		//goodsBaseInfo.setGoods_id(goodsInfoList.get(0).getGoods_id());
		goodsBaseInfo.setArea_id(areaId);
		goodsBaseInfo.setUser_id(userId);
		goodsBaseInfo.setPay_type_id(payTypeId);
     //
		//商品分類ID(0:ガイド商品, 1:資源商品)
		goodsBaseInfo.setGoods_type_id(0);
		goodsBaseInfo.setClick_count(0);
		goodsBaseInfo.setIs_hot(true);
		goodsBaseInfo.setIs_new(true);
		goodsDao.insertGoodsBaseInfo(goodsBaseInfo);

		//商品詳細
		for(GoodsInfoDto goodsLanguageInfo : goodsInfoList){
		//	goodsLanguageInfo.setGoods_id(goodsId);	
			goodsDao.insertGoodsLanguageInfo(goodsLanguageInfo);
		}

		//商品と資源商品分類関連
		if(selectedResourceTypes!=null && !selectedResourceTypes.isEmpty()){
			String resourceTypes[] = selectedResourceTypes.split(",");
			for(String resourceTypeId : resourceTypes){
				if(resourceTypeId!=null && !resourceTypeId.isEmpty()){
					GoodsInfoDto goodsResourceTypeR = new GoodsInfoDto();
			        goodsResourceTypeR.setGoods_id(goodsId);
					goodsResourceTypeR.setResource_type_id(Integer.valueOf(resourceTypeId));
					goodsDao.insertGoodsResourceTypeInfo(goodsResourceTypeR);
				}
			}
		}
		
		
		return null;
	}

	//画面の商品情報を登録
	public void updateGoodsInfo(List<GoodsInfoDto> goodsInfoList,String goodsId,String areaId,String selectedResourceTypes,int payTypeId){
		//商品表
		GoodsInfoDto goodsBaseInfo = new GoodsInfoDto();
		goodsBaseInfo.setGoods_id(goodsId);
		goodsBaseInfo.setArea_id(areaId);
		goodsBaseInfo.setPay_type_id(payTypeId);
		goodsDao.updateGoodsBaseInfo(goodsBaseInfo);

		//商品詳細
		for(GoodsInfoDto goodsLanguageInfo : goodsInfoList){
			//商品情報存在性判定
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("goods_id", goodsLanguageInfo.getGoods_id());
			param.put("language_id", goodsLanguageInfo.getLanguage_id());

			int count = goodsDao.checkGoodsByGoodsIdAndLanguage(param);
			if(count>0){
				//商品情報存在場合、更新する
				goodsDao.updateGoodsLanguageInfo(goodsLanguageInfo);
			}else{
				//商品情報存在しない場合、追加する
				goodsDao.insertGoodsLanguageInfo(goodsLanguageInfo);
			}
		}
		
		
		//商品と資源商品分類関連
		goodsDao.deleteGoodsResourceTypeInfo(goodsId);
		
		if(selectedResourceTypes!=null && !selectedResourceTypes.isEmpty()){
			String resourceTypes[] = selectedResourceTypes.split(",");
			for(String resourceTypeId : resourceTypes){
				if(resourceTypeId!=null && !resourceTypeId.isEmpty()){
					GoodsInfoDto goodsResourceTypeR = new GoodsInfoDto();
					goodsResourceTypeR.setGoods_id(goodsId);
					goodsResourceTypeR.setResource_type_id(Integer.valueOf(resourceTypeId));
					goodsDao.insertGoodsResourceTypeInfo(goodsResourceTypeR);
				}
			}
		}

	}

	//商品登録画面に、選択した商品情報を検索する
	public List<GoodsInfoDto> getGoodsListByGoodsId(String goods_id){
		return goodsDao.selectGoodsListByGoodsId(goods_id);
	}
	/**
	 * @return the goodsDao
	 */
	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	/**
	 * @param goodsDao the goodsDao to set
	 */
	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}

	//Home Page画面
	public List<GoodsInfoDto> selectHomePageGoods() {
		// TODO Auto-generated method stub
		return goodsDao.selectHomePageGoods();
	}


	//资源登録画面に、該当ユーザーの全部商品情報を検索する
	public List<ResourceTypeInfoDto> getResourceListInfo(int resource_type_id){
		return goodsDao.selectResourceListInfo(resource_type_id);
	}

	public List<GoodsInfoDto> getResourceListByGoodsId(Map<String,Object> param){
		return goodsDao.selectResourceListByGoodsId(param);
	}

	public List<ResourceTypeInfoDto> getResourceTypeListInfo(){
		return goodsDao.selectAllResourceType();
	}

	public List<GoodsInfoDto> getGoodsListByGoodsIdLanguageId(String goods_id,
			String languageId) {
		// TODO Auto-generated method stub
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("goods_id", goods_id);
		param.put("language_id", languageId);
		return  goodsDao.selectGoodsListByGoodsIdLanguageId(param);
	}
	
	public List<GoodsInfoDto> getResorceGoodsListByUserId(String userid) {
		// TODO Auto-generated method stub

		return  goodsDao.selectResorceGoodsListByUserId(userid);
	}

}
